import IState from "./IState";
import eState from "./eState";
import StateEvent from "./StateEvent";
import EventDefines from "../../Constants/EventDefines";
import AnimationController from "../../Controllers/AnimationController";

/**
 * 状态机基类
 * created by Five on 2018-12-4 18:30:52
 */

export default class Machine implements IState {
    Ownner: AnimationController = null;
    Moveable: boolean = false;
    StateID: eState = eState.NONE;

    constructor(anim: AnimationController) {
        this.Ownner = anim;
    }

    Enter(se: StateEvent) {
        this.Ownner.stateEnter(this.newEvent(EventDefines.EVENT_FSM_ENTER, se));
    }

    Execute(se: StateEvent) {
        this.Ownner.stateExecute(this.newEvent(EventDefines.EVENT_FSM_ENTER, se));
    }

    Exit(se: StateEvent) {
        this.Ownner.stateExit(this.newEvent(EventDefines.EVENT_FSM_ENTER, se));
    }

    newEvent(ename: string, se: StateEvent) {
        let event = new cc.Event.EventCustom(ename, true);
        event.setUserData([this.StateID, se]);
        return event;
    }
}